﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class users_user_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request.UrlReferrer != null)
            {
                ViewState["history_back_page"] = Request.UrlReferrer.LocalPath;
            }
            else
            {
                ViewState["history_back_page"] = "";
            }

            this.BtnOK.Attributes.Add("onclick", "return check_submit()");

            HttpCookie cookie = Request.Cookies["bestone_pay"];
            if (cookie != null)
            {
                this.Txt_user_name.Text = Convert.ToString(cookie.Values["user_name"]);
                if (this.Txt_user_name.Text.Length > 0)
                {
                    this.ChkRememberPwd.Checked = true;
                }
            }
        }

        if (Request.QueryString["logout"] == "true")
        {
            Session["user_id"] = "";
            Session["user_name"] = "";
            Session["user_email"] = "";
        }
    }

    protected void BtnOK_Click(object sender, EventArgs e)
    {

        if (!SysCommon.CheckCodeSame(this.Txt_CheckCode.Text.Trim()))
        {
            SysCommon.JavaScript.alert("校验码错误！请重新输入！");
            return;
        }

        DataRow dr = DataBaseOperate_SQLServer.GetDataRow("select * from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "' and user_password='" + SysCommon.ProcessSqlStr(this.Txt_user_password.Text) + "'");

        if (dr != null)
        {
            Session["user_id"] = dr["user_id"];
            Session["user_name"] = dr["user_name"];
            Session["user_email"] = dr["email"];
            Session["lastlogin_time"] = dr["lastlogin_time"];

            if (ChkRememberPwd.Checked == true)
            {
                HttpCookie cookie = new HttpCookie("bestone_pay");
                cookie.Values["user_name"] = Convert.ToString(dr["user_name"]);

                cookie.Expires = DateTime.Now.AddDays(100);  //设置过期时间

                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = Request.Cookies["bestone_pay"];
                if (cookie != null)
                {
                    cookie.Values.Remove("user_name");
                    Response.Cookies.Add(cookie);
                }
            }

            DataBaseOperate_SQLServer.ExecUpdate("update t_user set lastlogin_time=getdate() where user_id=" + (int)dr["user_id"]);

            if (ViewState["history_back_page"].ToString().Length > 0)
            {
                Page.Response.Redirect(ViewState["history_back_page"].ToString());
            }
            else
            {
                Page.Response.Redirect("~/default.aspx");
            }
        }
        else
        {
            int rowCount = (int)DataBaseOperate_SQLServer.GetValue("select count(1) from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "'");

            if (rowCount == 0)
            {
                SysCommon.JavaScript.alert("用户名输入不正确！请重新输入。");
                return;
            }
            else
            {
                SysCommon.JavaScript.alert("密码输入不正确！请重新输入。");
                return;
            }
        }

    }
}
